/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import com.mysql.jdbc.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.bean.BeanCategoria;
import model.bean.BeanSemestre;
import model.daobase.DBase;

/**
 *
 * @author Jesus
 */
public class DaoCategoria extends DBase {
    
    public ArrayList<BeanCategoria> lista() {

        this.init_mysql();
        PreparedStatement pstmt = null;
        ResultSet res = null;
        ArrayList<BeanCategoria> listaCategorias = new ArrayList<BeanCategoria>();

        try {

            String query = "SELECT * FROM categoria";

            pstmt = (PreparedStatement) conn.prepareStatement(query);
            res = pstmt.executeQuery();
            BeanCategoria categoria;
            while (res.next())
            {
               categoria = new BeanCategoria();
               
               categoria.setIdcategoria(res.getInt(1));
               categoria.setNombre(res.getString(2));
               
                
               listaCategorias.add(categoria);
            }

            res.close();
            pstmt.close();
            conn.close();

        } catch (SQLException e1) {
            e1.getMessage();
        }

        return listaCategorias;

    }
    
    public String obtenerNombre(String idcategoria){
        String name=null;
        
        this.init_mysql();
        java.sql.PreparedStatement pstmt = null;
        ResultSet res = null;
        

        try {

            String query = "SELECT nombre FROM categoria"
                    + " WHERE idcategoria=" + idcategoria ;

            pstmt = conn.prepareStatement(query);
            res = pstmt.executeQuery();
            while (res.next()) {
                name=res.getString(1);
               

            }
            res.close();
            pstmt.close();
            conn.close();

        } catch (SQLException e1) {
            e1.getMessage();
        }
        
        return name;
    }
    
}
